Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix(copy): add support for copying Blob objects #14064

Closed
wants to merge 2 commits into from

Conversation

gkalpak
Copy link
Member

@gkalpak gkalpak commented Feb 17, 2016

Although copy() does not need to (and never will) support all kinds of objects, there is a (not uncommon) usecase for supporting Blob objects:

ngMock's $httpBackend will return a copy of the response data (so that changes in one test won't affect others). Since returning Blob objects in response to HTTP requests is a valid usecase and since ngMocks's $httpBackend will use copy() to create a copy of that data, it is reasonable to support Blob objects.
(I didn't run any benchmarks, but the additional check for the type of the copied element should have negligible impact, compared to the other stuff that copy() is doing.)

Fixes #9669

Although `copy()` does not need to (and never will) support all kinds of objects, there is a
(not uncommon) usecase for supporting `Blob` objects:

`ngMock`'s `$httpBackend` will return a copy of the response data (so that changes in one test won't
affect others). Since returning `Blob` objects in response to HTTP requests is a valid usecase and
since `ngMocks`'s `$httpBackend` will use `copy()` to create a copy of that data, it is reasonable
to support `Blob` objects.
(I didn't run any benchmarks, but the additional check for the type of the copied element should
have negligible impact, compared to the other stuff that `copy()` is doing.)

Fixes angular#9669
expect(dst).not.toBe(src);
expect(dst.size).toBe(3);
expect(dst.type).toBe('bar');
expect(isBlob(dst)).toBe(true);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you checked if this test perhaps passes and gives a false positive without the applied copyType() patch?

I seem to recall, when running into this issue first, that the cloned object was getting reported as a Blob even though it was not actually one, and I could only detect the difference by attempting to make a call to one of its specific functions like slice() or by attempting to read the blob's content by using the FileReader interface (either of which then failed in some internal implementation, in different ways depending on the browser used).

Update: I just checked it at http://plnkr.co/edit/xenwdKodooTrYO7VG2P2 and the test is good while my memory was mistaken. 👍 😄 The test will fail on 3 of those expect() calls - size access, type access & the isBlob() check. The thing I recalled incorrectly was that the typeof operator was returning Blob for the cloned object. Good work!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BTW, I don't think typeof returns Blob for blobs. In latest Chrome and Firefox at least, it return object.
(Maybe what you had in mind is that expect(typeof dst).toBe(typeof src) would indeed pass even without the fix (because both would return object).)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm certain I got Blob for natively created Blob objects with PhantomJS. Don't recall what actual user browsers gave me there. But it does not matter in the end. Should work like a charm now. 😺

@jurko-gospodnetic
Copy link
Contributor

Approved. Tested it, and it works as advertised for me. 👍

Not sure how you report review approvals here. 😄

Any chance this can be back-ported to the 1.4.x branch?

@lgalfaso
Copy link
Contributor

lgalfaso commented Feb 17, 2016 via email

@@ -1011,6 +1011,26 @@ describe('ngMock', function() {
});


it('should be able to handle Blobs as mock data', function() {
if (typeof Blob !== 'udefined') {
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

udefined (lol)

@gkalpak gkalpak closed this in 0b1b911 Feb 17, 2016
gkalpak added a commit that referenced this pull request Feb 17, 2016
Although `copy()` does not need to (and never will) support all kinds of objects, there is a
(not uncommon) usecase for supporting `Blob` objects:

`ngMock`'s `$httpBackend` will return a copy of the response data (so that changes in one test won't
affect others). Since returning `Blob` objects in response to HTTP requests is a valid usecase and
since `ngMocks`'s `$httpBackend` will use `copy()` to create a copy of that data, it is reasonable
to support `Blob` objects.
(I didn't run any benchmarks, but the additional check for the type of the copied element should
have negligible impact, compared to the other stuff that `copy()` is doing.)

Fixes #9669

Closes #14064
gkalpak added a commit that referenced this pull request Feb 17, 2016
Although `copy()` does not need to (and never will) support all kinds of objects, there is a
(not uncommon) usecase for supporting `Blob` objects:

`ngMock`'s `$httpBackend` will return a copy of the response data (so that changes in one test won't
affect others). Since returning `Blob` objects in response to HTTP requests is a valid usecase and
since `ngMocks`'s `$httpBackend` will use `copy()` to create a copy of that data, it is reasonable
to support `Blob` objects.
(I didn't run any benchmarks, but the additional check for the type of the copied element should
have negligible impact, compared to the other stuff that `copy()` is doing.)

Fixes #9669

Closes #14064
@gkalpak
Copy link
Member Author

gkalpak commented Feb 17, 2016

Backported to v1.5.x (e9d579b) and v1.4.x (863a423).

@gkalpak gkalpak deleted the fix-copy-add-support-for-Blob branch February 17, 2016 23:37
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants